package commandPattern;

import interpreter.ShapeBase;
import GUI.FuncPanel;
import GUI.ShapesPanel;

import composite.ObjectGroup;
import composite.ObjectItem;

/**
 * Add a new object (from shapeBase) to the rootNode, 
 * the type is taken from ShapesPanel,
 * the color from FuncPanel
 */
public class AddObject implements Command  {
	ObjectGroup rootGroup;
	public void setRootGroup(ObjectGroup rootGroup) {this.rootGroup = rootGroup;}

	ObjectItem newObject;
	
	int posX, posY;
	public void setPosX(int posX) { this.posX = posX; }
	public void setPosY(int posY) {	this.posY = posY; }

	public void execute() {
			ShapeBase shapeBase = ShapeBase.getInstance();
			
			// Create new object
			newObject = shapeBase.get_shape(ShapesPanel.currentShape);
			newObject.moveObject(posX, posY);
			newObject.setColor(FuncPanel.current_color);
			// Add the object
			rootGroup.addObjectItem(newObject);
	}

	public void redo() {
		rootGroup.addObjectItem(newObject);
	}

	public void undo() {
		rootGroup.removeObjectItem(newObject);
	}
	
	/** Return a clone of command at the state before execution */
	public AddObject clone(){
		AddObject commandClone = new AddObject();
		commandClone.setPosX(posX);
		commandClone.setPosY(posY);
		commandClone.setRootGroup(rootGroup);
		return commandClone;
	}
}
